﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using PQ.IBL;
using PQ.BL;
using PQ.Model;
using PQ.Enum;
using PQ.Helper;

namespace PQ
{
    public partial class FrmLogin : Form
    {
        /// <summary>
        /// 日志
        /// </summary>
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(FrmLogin));

        private IBLUser blUser;
        public IBLUser BlUser
        {
            get { return blUser; }
            set { blUser = value; }
        }

        public UserEntity userEntity { get; set; }
        /// <summary>
        /// 是否动了密码..
        /// 如果没动..取当前 下拉框的 密码值。
        /// 动了。取文本框的值
        /// </summary>
        private bool IsChangepwd = false;
        public FrmLogin()
        {
            InitializeComponent();
            blUser = new BLUser();
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            //this.txtUser.Text = "1497414463";
            //this.txtPass.Text = "abcdefghij";

            //this.txtUser.Text = "404313364";
            //this.txtPass.Text = "qsiofg4nui897574";
            IsChangepwd = false;
            List<UserEntity> userList= blUser.GetUsers();
            if (userList.Count <= 0)
            {
                this.txtPass.Text = string.Empty;
            }
            else
            {
                this.txtPass.Text = RandData.GetInstance().GetRandStr(16);
            }

            this.cbxUser.DataSource = userList;
            this.cbxUser.DisplayMember = "Uin";
            this.cbxUser.ValueMember = "Ppwwdd";
           

            this.cbxUser.Focus();
        }

       //密码改变事件
        private void txtPass_KeyPress(object sender, KeyPressEventArgs e)
        {
            IsChangepwd = true;
        }
  
        private void btnLogin_Click(object sender, EventArgs e)
        {
           
            UserEntity userEntity = new UserEntity();
            userEntity.UserName = this.cbxUser.Text;
            userEntity.Uin = userEntity.UserName;
            if (IsChangepwd==true)
            {
                userEntity.Password = this.txtPass.Text;
            }
            else
            {
                userEntity.Password = this.cbxUser.SelectedValue != null ? this.cbxUser.SelectedValue.ToString() : this.txtPass.Text;
            }
             
            StartLogin(userEntity);
        }

        /// <summary>
        /// 开始 登陆
        /// </summary>
        /// <param name="userEntity"></param>
        private void StartLogin(UserEntity userEntity)
        {
           
            bool isNeedVerifyCode = blUser.IsNeedVerifyCode(userEntity);
            if (isNeedVerifyCode)
            {
                FrmVerifyCode form = new FrmVerifyCode(blUser, userEntity);
                form.ShowDialog();
            }
            UserEntity resultUser = blUser.Login(userEntity);
            if (resultUser.Status == LoginStatus.VerifyCodewang)
            {
                StartLogin(userEntity);
            }
            else if (resultUser.Status == LoginStatus.Logined)
            {
                //FrmMain main = new FrmMain(this.blUser,userEntity);
                //main.Show();
                this.userEntity = userEntity;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("error:"+resultUser.Status.ToString());
            }
        }

        

       
    }
}
